package com.example.administrator.a32;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText edt;
    private ImageView img;
    private ProgressBar proBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.btn_test);
        Button btn2 = (Button) findViewById(R.id.btn_test2);

        btn.setOnClickListener(this);
        btn2.setOnClickListener(this);

        edt = (EditText) findViewById(R.id.edtTxt_test);
        img = (ImageView) findViewById(R.id.img_test);
        proBar = (ProgressBar) findViewById(R.id.proBar_test);
        proBar.setVisibility(View.GONE);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_test:

                proBar = new ProgressBar(MainActivity.this);
                String inputText = edt.getText().toString();

                Log.d("TAG", "inputText:" + inputText);

                if(inputText.equals("syt")) {
                    Toast.makeText(MainActivity.this, "输入正确:"+inputText, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this,"输入错误:"+inputText, Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.btn_test2:
                AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                dialog.setTitle("对话框");
                dialog.setMessage("这是对话内容-----------换图片吗？");
                dialog.setCancelable(true);//是否可以撤销
                dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(proBar.getVisibility() == View.GONE) {
                            proBar.setVisibility(View.VISIBLE);
                            img.setImageResource(R.drawable.test_2);
                        } else {
                            proBar.setVisibility(View.GONE);
                            img.setImageResource(R.drawable.test_1);
                        }
                    }
                });
                dialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.show();
                break;
                default:
                    break;
        }
    }
}
